//
//  DictionaryManager.m
//  LineByLine
//
//  Created by 병철 김 on 11. 12. 16..
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "DictionaryManager.h"
#import "CoreDataManager.h"


@implementation DictionaryManager
@synthesize delegate, dao;

+ (DictionaryManager*) sharedDictionaryManager {
    static DictionaryManager *instance;
    
    if (!instance) {
        @synchronized(self) {
            if (!instance) {
                instance = [[DictionaryManager alloc] init];
                DictionaryManagerDaoServer *serverDao = [[DictionaryManagerDaoServer alloc] init];
                DictionaryManagerDao *dao = [[DictionaryManagerDao alloc] init];
                [dao setServerDao:serverDao];
                [instance setDao:dao];
                
                [serverDao release];
                [dao release];
            }
        }
    }
    
    return instance;
}

- (void) simpleMeaning:(NSString*)word {
    [dao setDelegate:delegate];
    [dao simpleMeaning:word];
}

- (void) cancelSearching {
    [dao cancelSearching];
}
@end


@implementation DictionaryManagerDao 
@synthesize serverDao, delegate;

- (void)simpleMeaning:(NSString*)word {
    NSString *meaning = [[CoreDataManager sharedCoreDataManager] selectOneFrom:@"Dictionary" where:@"word=%@", word, nil];
    
    if (!meaning) {
        [serverDao setDelegate:delegate];
        [serverDao simpleMeaning:word];
    } else {
        [delegate didFinishDictionarySeachingWord:word meaning:meaning];
    }
}

- (void) cancelSearching {
    // do nothing
}
@end

@implementation DictionaryManagerDaoServer
- (void)simpleMeaning:(NSString*)word {
    [self cancelSearching];
    
    httpRequset = [[HttpRequest alloc] init];
    [httpRequset setFinishTarget:self action:@selector(didFinishServerDicionarySearchingWord:)];
    [httpRequset executeUrl:@"http://ac.endic.naver.com/ac?q_enc=utf-8&st=1" method:@"GET" params:@"q", word, nil];
}

- (void) didFinishServerDicionarySearchingWord:(HttpRequest*) _httpRequest {
    NSData *responseData = [_httpRequest responseData];
    id json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
    
    NSArray *items = [json valueForKey:@"items"];
    NSString *word = [[json valueForKey:@"query"] objectAtIndex:0];
    @try {
        id resultWord = [[[[items objectAtIndex:0] objectAtIndex:0] objectAtIndex:0] objectAtIndex:0];
        id resultMeaning = nil;
        if ([word isEqualToString:resultWord]) {
            resultMeaning = [[[[items objectAtIndex:0] objectAtIndex:0] objectAtIndex:1] objectAtIndex:0];
        }
        [self cancelSearching];
        [delegate didFinishDictionarySeachingWord:word meaning:resultMeaning];
    }
    @catch (NSException *exception) {
        [self cancelSearching];
        [delegate didFinishDictionarySeachingWord:word meaning:nil];
    }
}

- (void) cancelSearching {
    if (httpRequset) {
        [httpRequset close];
        [httpRequset release];
        httpRequset = nil;
    }
}

@end

